package edu.mercer.sse.app;

import java.util.List;

import com.google.common.base.Throwables;
import com.google.common.collect.Lists;

import edu.mercer.sse.errors.CriticalException;
import edu.mercer.sse.errors.ErrorLoggingVisitor;
import edu.mercer.sse.ldap.LDAPConnector;

public class App
{

	public static void main( final String[] commandLineArgs )
	{
		try
		{
			final List<String> options = Lists.newArrayList(commandLineArgs);
			
			if( options.contains("--sync") )
			{ 
				System.out.println( "--sync option used, attempting to " 
								  + "modify LDAP" );
				
				System.out.println( DataSync.publish() );

				return;
				
			}//if
			
			System.out.println( "--sync option not used, LDAP not " 
							  + "modified, preview only" );

			System.out.println( DataSync.preview() );
				
		}
		catch( final Throwable err )
		{
			String msg = "Error, check log files for details: " 
					+ new CriticalException(err).accept(
					  new ErrorLoggingVisitor());
			
			abend(msg);
			
		}
		finally
		{
			LDAPConnector.disconnect();
			
		}//try
		
	}//method

	
	public static void abend( final String msg )
	{
		System.err.println( msg );
		System.exit(1);
		
	}//method

	
	public static void abend( final Exception err )
	{
		abend( Throwables.getStackTraceAsString(err) );
		
	}//method

	
	public static void abend( final String msg, final Exception err )
	{
		abend( msg + " " + Throwables.getStackTraceAsString(err) );
		
	}//method
	
	
}//class
